<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 标题区域 -->
            <div class="login_title">
                <p>壹号官家管理系统</p>
            </div>
            <!-- 表单区域 -->
            <el-form  ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
                <!-- 用户名 -->
                <el-form-item prop="loginname">
                    <el-input v-model="loginForm.loginname" prefix-icon="el-icon-user"
                     placeholder="用户名" ></el-input>
                </el-form-item>
                <!-- 密码 -->
                 <el-form-item prop="password" >
                    <el-input v-model="loginForm.password" prefix-icon="el-icon-view"
                     placeholder="密码" type="password"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-button type="success" @click="login">登录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-row :gutter='15'>
                        <el-col :span="18" >
                            <router-link :to="''">注册账号</router-link>
                        </el-col>
                        <el-col :span="6" > 
                            <router-link :to="''">忘记密码?</router-link>
                        </el-col>
                    </el-row>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

import qs from 'qs'

export default {
    data(){
        return{
            // 登录表单数据绑定对象
            loginForm:{
                loginname:'',
                password:'',
            },
            // 表单验证规则
            loginFormRules:{
                loginname:[
                    { required: true, message: '请输入用户账号', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请选择用户密码', trigger: 'change' },
                    { min: 3, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        login(){
            this.$refs.loginFormRef.validate(async valid => {
                if(!valid) return;
                const res =await this.http.get('login',this.loginForm);
                // 是否登录后台
                 if(!res.token){
                     return this.$message.error("登录失败")
                 }else{
                      this.$message.success("登录成功");
                        console.log(res);
                        window.sessionStorage.setItem('token',res.token)
                        this.$router.push('/home')
                 }
                   

            })
        },
         register(){
            // alert('register');
            this.$router.push('/register')
        }
    }
}
</script>



<style scoped>
.login_container{
    background: #f3f3f5;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 360px;
    background: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
.login_title{
    margin: 0 auto;
    text-align: center;
    padding: 5px 0;
}
.login_title p{
    font-size: 22px;
    font-weight: bolder;
}
.login_form{
    position: absolute;
    bottom: 5px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    /* margin-top:10px; */
}
.el-button{
    display: inline-block;
    width: 410px;
    padding: 10px 0;
    color: #fff;
}

</style>

